(function ($) {
    var Builder = function (ele, options) {
        var _this = this;
        // 参数初始化
        this.initOpts = function () {
            _this.$container = ele;

            _this.opts = $.extend({}, _this.defaults, options);

            _this.opts.totalNum = Number(_this.opts.totalNum);
            _this.opts.pageSize = Number(_this.opts.pageSize);
            _this.opts.currentPage = Number(_this.opts.currentPage);
            _this.opts.showPage = Number(_this.opts.showPage);

            _this.opts.totalPage = Math.ceil(_this.opts.totalNum / _this.opts.pageSize);
        };
        // 渲染html
        this.renderHtml = function () {
            var html = [];
            var pages = _this.handlePage();

            for (var i = 0; i < pages.length; i++) {
                html.push(_this.createHtml('page', pages[i]));
            }

            _this.opts['prev'] && html.unshift(_this.createHtml('prev', _this.opts.currentPage - 1));
            _this.opts['first'] && html.unshift(_this.createHtml('first', 1));
            _this.opts['next'] && html.push(_this.createHtml('next', _this.opts.currentPage + 1));
            _this.opts['last'] && html.push(_this.createHtml('last', _this.opts.totalPage));

            if (_this.opts.wrapper) {
                _this.$container.html($(_this.opts.wrapper).html(html.join('')));
            } else {
                _this.$container.html(html.join(''));
            }
        };
        // 创建html标签字符串
        this.createHtml = function (type, number) {
            var html = _this.opts[type].replace(/{{page}}/g, number);
            return $('<p>').append($(html).attr({
                'page-role': type,
                'page-data': number
            })).html();
        };
        // 更改按钮状态
        this.changeState = function () {
            if (_this.opts.currentPage === 1) {
                _this.opts['first'] && $('[page-role=first]', _this.$container).addClass(_this.opts.disableClass);
                _this.opts['prev'] && $('[page-role=prev]', _this.$container).addClass(_this.opts.disableClass);
            }

            if (_this.opts.currentPage >= _this.opts.totalPage) {
                _this.opts['next'] && $('[page-role=next]', _this.$container).addClass(_this.opts.disableClass);
                _this.opts['last'] && $('[page-role=last]', _this.$container).addClass(_this.opts.disableClass);
            }
            $('[page-role=page]', _this.$container).removeClass(_this.opts.activeClass);
            $('[page-role=page][page-data=' + _this.opts.currentPage + ']', _this.$container).addClass(_this.opts.activeClass);
        };
        // 按钮绑定事件
        this.bindEvent = function () {
            _this.$container.off();
            _this.$container.on('click', '[page-role]', function () {
                var $el = $(this);
                if ($el.hasClass(_this.opts.disableClass) || $el.hasClass(_this.opts.activeClass)) {
                    return;
                }
                _this.opts.currentPage = +$el.attr('page-data');
                _this.opts.onPageChange(_this.opts.currentPage);
                _this.render();
            });
        };
        // 页码算法
        this.handlePage = function () {
            var pages = [],
                showPage = _this.opts.showPage,
                currentPage = _this.opts.currentPage,
                totalPage = _this.opts.totalPage;

            if (showPage > totalPage) {
                showPage = totalPage;
            }

            var half = Math.floor(showPage / 2);
            var start = currentPage - half + 1 - showPage % 2;
            var end = currentPage + half;

            if (start < 1) {
                start = 1;
                end = showPage;
            }
            if (end > totalPage) {
                end = totalPage;
                start = 1 + totalPage - showPage;
            }

            var currentStart = start;
            while (currentStart <= end) {
                pages.push(currentStart);
                currentStart++;
            }
            return pages;
        };

        this.render = function () {
            _this.renderHtml();
            _this.changeState();
            _this.bindEvent();
        };
    };

    // 默认参数
    Builder.prototype.defaults = {
        wrapper: '',
        first: '<li class="paginator__first"><a href="javascript:;">首页</a></li>',
        prev: '<li class="paginator__prev"><a href="javascript:;">上一页</a></li>',
        next: '<li class="paginator__next"><a href="javascript:;">下一页</a></li>',
        last: '<li class="paginator__last"><a href="javascript:;">尾页</a></li>',
        page: '<li class="paginator__page"><a href="javascript:;">{{page}}</a></li>',
        totalNum: 0,
        pageSize: 0,
        currentPage: 1,
        showPage: 7,
        disableClass: 'disabled',
        activeClass: 'active',
        onPageChange: null,
    }

    // 初始化
    Builder.prototype.init = function () {
        this.initOpts();
        this.render();
    }

    $.fn.paginator = function (options) {
        builder = new Builder(this, options);
        return builder.init();
    }
})(jQuery);